package array;

public class 需要排序的最小长度 {
	public static int getMinLength(int[] arr){
		if(arr==null || arr.length<2) {
			return 0;
		}
		int min = arr[arr.length-1];
		int noMinIndex = -1;
		for(int i=arr.length-2;i!=-1;i--) {
			if(arr[i]> min){
				noMinIndex= i;
				//System.out.print("noMinIndex: " +noMinIndex);
			}else{
				min = Math.min(min, arr[i]);
				System.out.print("min: " +min);
			}
		}
		
		//如果noMinIndex=-1,说明原来就是有序的
		if(noMinIndex ==-1) {
			return 0;
		}
		System.out.println("min: " +min + " \t noMinIndex: " +noMinIndex );
		
		int max =arr[0];
		int noMaxIndex = -1;
		for(int i=1;i<arr.length;i++) {
			if(arr[i]<max) {
				noMaxIndex  =i;
			}else {
				max = Math.max(max, arr[i]);
			}
		}
		
		System.out.println("max: " +max + " \t noMaxIndex: " +noMaxIndex );
		return noMaxIndex - noMinIndex +1;
	}
	
	public static void main(String[] args) {
		int[] arr = { 1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19 };
		System.out.println(getMinLength(arr));

	}

}
